<template>
    <div>
        <h3>新增会员</h3>
        <table class="table table-bordered">
            <tbody>
                <tr>
                    <td align="right">会员编号</td>
                    <td>
                        <input type="text" v-model="queryInfo.MemberCode">
                    </td>
                </tr>

                <tr>
                    <td align="right">手机号</td>
                    <td>
                        <input type="text" v-model="queryInfo.Phone" maxlength="11">
                    </td>
                </tr>

                <tr>
                    <td align="right">姓名</td>
                    <td>
                        <input type="text" v-model="queryInfo.UesrName">
                    </td>
                </tr>

                <tr>
                    <td align="right">身份证号</td>
                    <td>
                        <input type="text" v-model="queryInfo.Card" maxlength="18">
                    </td>
                </tr>

                <tr>
                    <td align="right">会员等级</td>
                    <td>
                        <select v-model="queryInfo.LevelId">
                            <option value="0">请选择</option>
                            <option :value="item.Id" v-for="item in data.list">{{item.LevelName }}</option>
                        </select>
                    </td>
                </tr>

                <tr>
                    <td align="right">会员生日</td>
                    <td>
                        <input type="date" v-model="queryInfo.Birthday"/>
                    </td>
                </tr>

                <tr>
                    <td align="right">性别</td>
                    <td>
                        <input type="radio" name="sex" :value="1" v-model="queryInfo.sex">男
                        <input type="radio" name="sex" :value="0" v-model="queryInfo.sex">女
                    </td>
                </tr>


                <tr>
                    <td align="right">积分</td>
                    <td>
                        <input type="text" v-model="queryInfo.JiFen">
                    </td>
                </tr>

                
                <tr>
                    <td align="right">余额</td>
                    <td>
                        <input type="text" v-model="queryInfo.Money">
                    </td>
                </tr>

                <tr>
                    <td align="right"></td>
                    <td>
                        <input type="button" value="增加" @click="ok" class="btn btn-primary">
                    </td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">

import axios from 'axios';
import { ref,reactive,onMounted } from 'vue';
import { useRouter } from 'vue-router';
import moment from 'moment';

onMounted(()=>{
    getList();
})

//存储等级列表
let data:any= reactive({
    list:[]
})

//绑定下拉框
const getList=()=>{
    axios({
        method:'get',
        url:'/api/Member/GetMemberLevelList',
    })
    .then(res=>{
        console.log(res);
        data.list=res.data;
    })
    .catch(err=>{
        console.log(err);
    })
}


// var num=parseInt(Math.random()*1000+100);
var num=Math.random().toString().substring(2,5);
console.log(num)
const router=useRouter();

let queryInfo:any=reactive({
  "MemberCode": moment(new Date()).format("YYYYMMDD")+num,
  "Phone": "",
  "UesrName": "",
  "Card": "",
  "LevelId": 0,
  "Birthday": "",
  "Sex":"0",
  "JiFen": '',
  "Money": ''
})

const ok=()=>{
    //1.有效验证
    if(queryInfo.Phone==""){
        alert('手机号不能为空');
        return;
    }
    let reg=/^1[3-9]\d{9}$/;
    if(reg.test(queryInfo.Phone)==false){
        alert('手机号不合法');
        return;
    }
    if(queryInfo.useName==""){
        alert('姓名不能为空');
        return;
    }

    if(queryInfo.Card==""){
        alert('身份证号不能为空');
        return;
    }
    let reg1=/^\d{17}[0-9xX]$/;
    if(reg1.test(queryInfo.Card)==false){
        alert('身份证号格式不正确')
        return;
    }

    if(queryInfo.LevelId==""){
        alert('等级必选');
        return;
    }
    if(queryInfo.Birthday==""){
        alert('生日不能为空');
        return;
    }
    if(queryInfo.JiFen==""){
        alert('积分不能为空');
        return;
    }
    if(isNaN(queryInfo.JiFen)){
        alert('积分只能为数值');
        return;
    }
    if(queryInfo.Money==""){
        alert('余额不能为空');
        return;
    }
    if(isNaN(queryInfo.Money)){
        alert('余额必须是数值');
        return;
    }

  
    axios({
        method:"post",
        url:'/api/Member/MemberAdd',
        data:queryInfo
    })
    .then(res=>{
        console.log(res);
        if(res.data==-1){
            alert('身份证号重复')
        }
        else if(res.data>0){
            alert('添加成功');
        }
        else{
            alert('添加失败');
        }
      
    })
    .catch(err=>{
        console.log(err);
    })
}


















</script>

















<style scoped>

</style>